package com.rent.Impl;
import com.rent.utils.*;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.rent.bean.Rent;
import com.rent.bean.User;
import com.rent.bean.Vip;
import com.rent.dao.*;

public class VipDaoImpl extends BaseDao implements VipDao {
	
	DataAccessUtils d = new DataAccessUtils();
	
	@Override
	public List<Vip> seleByUname(String uname) {
		List<Vip> li=new ArrayList<Vip>();
		String sql="select * from vip where uname=? order by limittime desc limit 1";
		ResultSet re=super.executeQuery(sql, uname);
		try {
			while(re.next()) {
				Vip vip=new Vip(re.getInt(1), re.getString(2), re.getString(3), re.getString(4));
				li.add(vip);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO 自动生成的方法存根
		return li;
	}

	@Override
	public void newVip(User us, Vip v) {
		
		String sql="insert into vip values(null,?,?,?)";
		Object [] obj= {v.getUname(),v.getOpentime(),v.getLimittime()};
		super.executeUpdate(sql, obj);//开会员信息持久化
	}

	@Override
	public Vip shicha(String name) {
		// TODO Auto-generated method stub
		Vip vip=null;
		String sql="select * from vip where uname = ? order by limittime desc limit 1";
		ResultSet rs=super.executeQuery(sql,name);
		try {
			if(rs.next()) {
				 vip=new Vip(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4));
			
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return vip;
		
	}
	/**
	 * 会员查询
	 */
	@Override
	public List<Vip> huiUname(String uname) {
		List<Vip> li=new ArrayList<Vip>();
		String sql="select * from vip where uname=?";
		ResultSet re=super.executeQuery(sql, uname);
		try {
			while(re.next()) {
				Vip vip=new Vip(re.getInt(1), re.getString(2), re.getString(3), re.getString(4));
				li.add(vip);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO 自动生成的方法存根
		return li;
	}

	@Override
	public int topup(User us) {
		// TODO Auto-generated method stub
		String sql="update user set balance=balance+? where uname=?";
		int s=super.executeUpdate(sql,us.getMoney(), us.getUname());
		return 0;
	}

	@Override
	public List<User> cha(String name) {
		// TODO Auto-generated method stub
		List<User> list=new ArrayList<>();
		String sql="select uname,balance from User";
		ResultSet rs=super.executeQuery(sql);
		try {
			while(rs.next()) {
				User us=new User(rs.getString(1), rs.getDouble(2));
				list.add(us);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Vip seleByUnameOrderLimittime(String uname) {
		String sql = "select * from vip where uname = ? order by limittime desc;";
		Vip vip = null;
		ResultSet rs = executeQuery(sql, uname);
		try {
			if(rs.next()) {
				vip = new Vip(rs.getInt("vid"),rs.getString("uname"),rs.getString("opentime"),rs.getString("limittime"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeAll(rs);
		}
		return vip;
	}

	@Override
	public List<User> seleVip() {
		String sql = "select * from user_vip where limittime>now()";
		List<User> list = null;
		ResultSet rs = executeQuery(sql);
		try {
			list = d.parseUserByVip(rs);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeAll(rs);
		}
		return list;
	}

	@Override
	public List<User> seleVipExpire() {
		String sql = "select * from user_vip where limittime<now()";
		List<User> list = null;
		ResultSet rs = executeQuery(sql);
		try {
			list = d.parseUserByVip(rs);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeAll(rs);
		}
		return list;
	}

	@Override
	public int seleCount() {
		String sql = "select count(1) from user_vip where limittime>now()";
		int count = 0;
		ResultSet rs = executeQuery(sql);
		try {
			if(rs.next()) {
				count = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}

	
	
}
